package com.macro.mall.tiny.modules.bms.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.bms.dto.AdvertQueryParam;
import com.macro.mall.tiny.modules.bms.model.Advert;
import com.macro.mall.tiny.modules.bms.service.AdvertService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Tag(name = "AdvertController", description = "广告管理")
@RestController
@RequestMapping("/advert")
public class AdvertController {

    @Autowired
    private AdvertService advertService;

    @Operation(summary = "根据ID获取广告详情")
    @Parameters({
            @Parameter(name = "id", description = "广告ID")
    })
    @GetMapping("/{id}")
    public CommonResult<Advert> getById(@PathVariable Long id) {
        Advert advert = advertService.getById(id);
        if (advert == null || advert.getDelFlag() == 1) {
            return CommonResult.failed("广告不存在");
        }
        return CommonResult.success(advert);
    }

    @Operation(summary = "分页获取广告列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<Advert>> getList(AdvertQueryParam queryParam) {
        CommonPage<Advert> pageList = advertService.getPageList(
                queryParam.getPositionId(),
                queryParam.getName(),
                queryParam.getStartTime(),
                queryParam.getEndTime(),
                queryParam.getPageNum(),
                queryParam.getPageSize());
        return CommonResult.success(pageList);
    }

    @Operation(summary = "根据广告位ID获取所有有效广告")
    @Parameters({
            @Parameter(name = "positionId", description = "广告位ID")
    })
    @GetMapping("/active/list")
    public CommonResult<List<Advert>> getActiveAdvertsByPositionId(@RequestParam Integer positionId) {
        List<Advert> advertList = advertService.getActiveAdvertsByPositionId(positionId);
        return CommonResult.success(advertList);
    }

    @Operation(summary = "新增广告")
    @PostMapping("/create")
    public CommonResult<Object> create(@RequestBody Advert advert) {
        boolean result = advertService.create(advert);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "更新广告")
    @PutMapping("/update/{id}")
    public CommonResult<Object> update(@PathVariable Long id, @RequestBody Advert advert) {
        boolean result = advertService.update(id, advert);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "删除广告")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Object> delete(@PathVariable Long id) {
        boolean result = advertService.delete(id);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }
}